<template>
    <div class='approval'>
      <el-card class="box-card" style="margin-top: 110px;">
        <div class='title'>
          {{$t('achievements.title1')}}
        </div>
        <div class='menu-search'> 
            <div class='menu-title'>
                <!-- 客户姓名 -->
                <div class='searchItem'>
                    <span>{{$t('achievements.realName')}}</span>
                    <el-input v-model='listQuery.realName' :placeholder="$t('rule.realName')" size='small'></el-input>
                </div>
                <!-- 手机号 -->
                <div class='searchItem'>
                    <span>{{$t('achievements.phoneNo')}}</span>
                    <el-input v-model='listQuery.phoneNo' :placeholder="$t('rule.phoneNo')" size='small'></el-input>
                </div>
                <!-- 申请状态 -->
                <div class='searchItem'>
                    <span>{{$t('customQuery.authStatus')}}</span>
                    <el-select v-model='listQuery.authStatus' size='small'>
                          <el-option
                          v-for='(item, index) in apply'
                          :key='index'
                          :label='item.label'
                          :value='item.value'>
                          </el-option>
                      </el-select>
                </div>
                <!-- 注册时间 -->
                <div class='searchItem searchItem-date'>
                    <span>{{$t('customQuery.customerCrtTime')}}</span>
                    <el-date-picker
                    size='small'
                    v-model="listQuery.beginDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    :placeholder="$t('rule.beginDate')">
                    </el-date-picker>
                    <b>{{$t('approval.go')}}</b>
                    <el-date-picker
                    size='small'
                      v-model="listQuery.endDate"
                      type="date"
                      value-format="yyyy-MM-dd"
                      :placeholder="$t('rule.endDate')">
                    </el-date-picker>
                </div>
            </div>
            <div class='menu-title-btn'>
                <button type='button' @click='search'>
                    {{$t('btn.query')}}
                </button>
                <button type='button' @click='reset'>
                    {{$t('btn.reset')}}
                </button>
                <button type='button' :disabled="expDis" @click="handleDownload" v-if="customQuery_btn_export">
                    {{$t('btn.export')}}
                </button>
                <button type='button' :disabled="expDis" @click="handleViewExcelUrl" v-if="customQuery_btn_black_export">
                    {{$t('loanDetails.cxDcdz')}}
                </button>
                <!-- 黑名单查询 -->
                <button type='button' @click="handleBlackSearch" v-if="customQuery_btn_black">
                    {{$t('btn.blackSearch')}}
                </button>
            </div>
        </div>
      </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
          <div class='title'>
              {{$t('achievements.title2')}}
          </div>
          <el-table
              v-loading="listLoading"
              ref='multipleTable'
              :data='tableData'
              tooltip-effect='dark'
              style='width: 100%'
              border
              stripe
              size='mini'>
              <!-- 用户ID -->
              <el-table-column align="center" header-align="center" 
              :label="$t('customQuery.userId')">
              <template slot-scope="scope">
                  <span class="href" @click="hrefFn(scope.row)">{{scope.row.userId}}</span>
              </template>
              </el-table-column>
              <!-- 客户姓名 -->
              <el-table-column
              prop='realName' align="center" header-align="center" 
              :label="$t('achievements.realName')">
              </el-table-column>
              <!-- 手机号 -->
              <el-table-column
              prop='userName' align="center" header-align="center" 
              :label="$t('achievements.phoneNo')">
              </el-table-column>
              <!-- 申请状态 -->
              <el-table-column
              prop='authStatus' align="center" header-align="center" 
              :label="$t('customQuery.authStatus')">
              <template slot-scope="scope">
                <span v-if="scope.row.authStatus=== '1'">{{$t('customQuery.authStatus1')}}</span>
                <span v-if="scope.row.authStatus=== '2'">{{$t('customQuery.authStatus2')}}</span>
                <span v-if="scope.row.authStatus=== '3'">{{$t('customQuery.authStatus3')}}</span>
            </template>
              </el-table-column>
              <!-- 注册时间 -->
              <el-table-column
              prop='regTime' align="center" header-align="center" 
              :label="$t('customQuery.customerCrtTime')">
              </el-table-column>
              <!-- 系统渠道 -->
              <el-table-column
              prop='channelName' align="center" header-align="center" 
              :label="$t('customQuery.systemChannel')">
              </el-table-column>
              <!-- 其他参数 -->
              <el-table-column 
                header-align='center'
                align='center'
                :label="$t('dispatch.others')"
                width="100" fixed="right" > 
                    <template slot-scope="scope">
                        <!-- 查看 -->
                        <el-button type="text" size="small" @click="handleClick(scope.row)">{{$t('approvalDetail.hitsee')}}</el-button>
                    </template> 
                </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listQuery.page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="listQuery.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
          </div>
        </el-card>
        <el-dialog
        :title="$t('dispatch.others')"
        :visible.sync="centerDialogVisible"
        width="60%"
        center>
        <div>
          <el-table
              :data="customerInfoDetail"
              border
              stripe
              style="width: 100%">
              <!-- 身份证号 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='identityNo'
              :label="$t('customQuery.cardNum')"
              width='200'>
              </el-table-column>
              <!-- 出生日期 -->
              <el-table-column
              header-align='center'
              align='center'
              prop="birthday"
              :label="$t('customQuery.certifiedDate')"
              width='140'>
              </el-table-column>
              <!-- 性别 -->
              <el-table-column
              header-align='center'
              align='center'
              prop="sex"
              :label="$t('user.sex')"
              width='140'>
              </el-table-column>
              <!-- 年龄 -->
              <el-table-column
              header-align='center'
              align='center'
              prop="age"
              :label="$t('RiskManager.age')"
              width='140'>
              </el-table-column>
              <!-- 职业 -->
              <el-table-column
              header-align='center'
              align='center'
              prop="worker"
              :label="$t('approvalDetail.Occupation')"
              width='140'>
              </el-table-column>
              <!-- 实际地址(省) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='customerProvince'
              :label="$t('customQuery.ActAddressProvince')"
              width='140'>
              </el-table-column>
              <!-- 实际地址(市) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='customerCity'
              :label="$t('customQuery.ActAddressCity')"
              width='140'>
              </el-table-column>
              <!-- 实际地址(区) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='customerArea'
              :label="$t('customQuery.ActAddressArea')"
              width='140'>
              </el-table-column>
              <!-- 身份证(省) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='identityProvince'
              :label="$t('customQuery.IDProvince')"
              width='140'>
              </el-table-column>
              <!-- 身份证(市) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='identityCity'
              :label="$t('customQuery.IDCity')"
              width='140'>
              </el-table-column>
              <!-- 身份证(区) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='identityArea'
              :label="$t('customQuery.IDArea')"
              width='140'>
              </el-table-column>
              <!-- 学历 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='education'
              :label="$t('customQuery.education')"
              width='100'>
              </el-table-column>
              <!-- 婚姻状况 -->
              <el-table-column
              header-align='center'
              align='center'
              prop="marriage"
              :label="$t('customQuery.marriage')"
              width='120'>
              </el-table-column>
              <!-- 公司名称 -->
              <el-table-column
              header-align='center'
              align='center'
              prop="companyName"
              :label="$t('approvalDetail.companyName')"
              width='120'>
              </el-table-column>
              <!-- 公司电话 -->
              <el-table-column
              header-align='center'
              align='center'
              prop="companyTel"
              :label="$t('approvalDetail.companyTel')"
              width='120'>
              </el-table-column>
              <!-- 工作年限 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='workYear'
              :label="$t('customQuery.year')"
              width='120'>
              </el-table-column>
              <!-- 公司地址(省) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='companyProvince'
              :label="$t('customQuery.CpnAddressProvince')"
              width='140'>
              </el-table-column>
              <!-- 公司地址(市) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='companyCity'
              :label="$t('customQuery.CpnAddressCity')"
              width='140'>
              </el-table-column>
              <!-- 公司地址(区) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='companyArea'
              :label="$t('customQuery.CpnAddressArea')"
              width='140'>
              </el-table-column>
          </el-table>
        </div>
        <div class="dialog-footer" style="text-align: center;padding-top: 20px;padding-bottom: 10px;">
            <el-button type="primary" @click="centerDialogVisible = false">{{$t('btn.close')}}</el-button>
        </div>
        </el-dialog>
        <!--黑名单查询dialog-->
        <el-dialog
        :title="$t('btn.blackSearch')"
        :visible.sync="blackSearchDialogVisible"
        :before-close="closeDialog"
        width="30%"
        center>
        <div>
          <el-tabs v-model="activeTab" @tab-click="handleToggleTab">
            <!-- 手机号 -->
            <el-tab-pane :label="$t('approval.phoneNo')" name="first">
              <el-form 
                :model="blackSearchForm" 
                :rules="blackSearchFormRules"
                :inline="true"
                style="margin:20px 0;"
                ref="form1"
                >
                <el-form-item label="" prop="mobile" >
                  <el-input style="width: 200px;" v-model="blackSearchForm.mobile" :placeholder="$t('rule.phoneNo')"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :loading="blackSearchLoading" @click="doBlackSearch('form1')">{{$t('btn.query')}}</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <!-- 身份证号 -->
            <el-tab-pane :label="$t('customQuery.cardNum')" name="second">
              <el-form 
                :model="blackSearchForm" 
                :rules="blackSearchFormRules"
                :inline="true"
                style="margin:20px 0;"
                ref="form2"
                >
                <el-form-item label="" prop="idNo" >
                  <el-input style="width: 200px;"  v-model="blackSearchForm.idNo" :placeholder="$t('rule.idNo')"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :loading="blackSearchLoading" @click="doBlackSearch('form2')">{{$t('btn.query')}}</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </div>
        </el-dialog>
        <!-- 查看Excel URL -->
      <el-dialog
      :title="$t('loanDetails.cxDcdz')"
      :visible.sync="urlDialogVisible"
      width="60%"
      center>
      <div style="padding-top: 20px;">
        <el-table
            :data="urlList"
            border
            stripe
            style="width: 100%">
            <!-- 文件名-->
            <el-table-column
            header-align='center'
            align='center'
            prop='fileName'
            :label="$t('loanDetails.wJm')">
            </el-table-column>
            <!-- 地址 -->
            <el-table-column
            header-align='center'
            align='center'
            :label="$t('loanDetails.xZdz')">
            <template slot-scope="scope">
              <a :href="scope.row.href" class="href" target="_blank">{{$t('dispatch.download')}}</a>
            </template>
            </el-table-column>
        </el-table>
      </div>
      <div class="dialog-footer" style="text-align: center;padding-top: 20px;padding-bottom: 10px;">
          <el-button type="primary" @click="urlDialogVisible = false">{{$t('btn.close')}}</el-button>
      </div>
      </el-dialog>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { parseURL, dataMasking } from 'utils/index';
import { page, customerDetail, getCustomerInfoDetail, checkHitBlackList, getOrderIdUuidByUserId, findExcelAddress } from 'api/customQuery/index'
export default {
  data() {
    return {
      urlDialogVisible: false,
      customerInfoDetail: [],
      centerDialogVisible: false,
      customQuery_btn_export: false,
      customQuery_btn_black_export: false,
      customQuery_btn_black: false,
      expDis: true,
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        realName: '',
        phoneNo: '',
        authStatus: '',
        beginDate: '',
        endDate: ''
      },
      tableData: [],
      apply: [
        {
          value: '',
          label: this.$t('rule.choose')
        },
        {
          value: 1,
          label: this.$t('customQuery.authStatus1')
        },
        {
          value: 2,
          label: this.$t('customQuery.authStatus2')
        },
        {
          value: 3,
          label: this.$t('customQuery.authStatus3')
        }],
      lanStatus: null,
      blackSearchDialogVisible: false,
      activeTab: 'first',
      blackSearchForm: {
        idNo: '',
        mobile: '',
        bankNo: ''
      },
      blackSearchFormRules: {
        idNo: [
          {
            required: true,
            message: this.$t('rule.idNo')
          }
        ],
        mobile: [
          {
            required: true,
            message: this.$t('rule.phoneNo')
          }
        ]
      },
      blackSearchLoading: false,
      urlList: []
    };
  },
  created() {
    if (this.language === 'zh') {
      this.lanStatus = 1;
    } else if (this.language === 'en') {
      this.lanStatus = 2;
    } else {
      this.lanStatus = 3;
    }
    if (this.customPage.has(this.$route.path)) {
      this.listQuery = this.customPage.get(this.$route.path)
    }
    this.getTime();
    this.getList();
    this.customQuery_btn_export = this.elements['customQuery:btn_export'];
    this.customQuery_btn_black_export = this.elements['customQuery:btn_black_export']
    this.customQuery_btn_black = this.elements['customQuery:btn_black'];
    // console.log(this.customQuery_btn_black)
  },
  computed: {
    ...mapGetters(["elements", "language", "customPage"])
  },
  methods: {
    getTime() {
      this.listQuery.beginDate = this.GetDateStr(-6);
      this.listQuery.endDate = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      let dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      let y = dd.getFullYear();
      let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1); // 获取当前月份的日期，不足10补0
      let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m + '-' + d;
    },
    // 查看
    handleClick(row) {
      const obj = {
        userId: row.userId
      }
      getOrderIdUuidByUserId(obj)
      .then(res => {
        if (res.status === 200 && res.data) {
          let data = {};
          data.userId = row.userId;
          data.userUuid = res.data.userUuid;
          data.language = this.lanStatus;
          getCustomerInfoDetail(data)
          .then(res => {
            if (res.status === 200) {
              this.customerInfoDetail = [];
              this.customerInfoDetail.push(res.data);
              this.centerDialogVisible = true
            }
          })
        }
      })
    },
    // 点击用户ID
    hrefFn(row) {
      const obj = {
        userId: row.userId
      }
      getOrderIdUuidByUserId(obj)
      .then(res => {
        if (res.status === 200 && res.data) {
          row.userUuid = res.data.userUuid;
          row.orderId = res.data.orderId;
          this.$store.dispatch('setCustQueryDetail', row)
          this.$router.push({
            path: '/custQueryDetail', 
            name: '客户详情'
          }) 
        }
      })
    },
    // 执行导出后台
    handleDownload() {
      this.listLoading = true;
      this.listQuery.language = this.lanStatus;
      customerDetail(this.listQuery)
      .then(res => {
        if (res.status === 200) {
          this.listLoading = false
          this.$message({ message: this.$t('dispatch.exportWarn'), type: 'success' })
          // res.data.forEach(item => {
          //   if (item.realName || item.userName) {
          //     item.realName = dataMasking(item.realName)
          //     item.userName = dataMasking(item.userName)
          //   }
          //   //申请状态
          //   switch (item.authStatus) {
          //     case '1':
          //       item.authStatus = this.$t('customQuery.authStatus1')
          //       break;
          //     case '2':
          //       item.authStatus = this.$t('customQuery.authStatus2')
          //       break;
          //     case '3':
          //       item.authStatus = this.$t('customQuery.authStatus3')
          //       break;
          //   }
          // })
          // require.ensure([], () => {
          //   const { export_json_to_excel } = require('@/vendor/Export2Excel')
          //   const tHeader = [ this.$t('customQuery.userId'), this.$t('achievements.realName'), this.$t('achievements.phoneNo'), this.$t('customQuery.authStatus'), this.$t('customQuery.customerCrtTime'), this.$t('customQuery.systemChannel') ];
          //   const filterVal = [ 'userId', 'realName', 'userName', 'authStatus', 'regTime', 'channelName' ];
          //   const list = res.data;
          //   const data = this.formatJson(filterVal, list)
          //   export_json_to_excel(tHeader, data, this.$t('route.customQuery'))
          //   this.listLoading = false;
          // })
        }
      })
    },
    // 查看导出URL
    handleViewExcelUrl() {
      this.listLoading = true
      findExcelAddress().then(res => {
        this.listLoading = false
        if (res.status === 200) {
          this.urlList = []
          const urls = res.data || []
          urls.forEach(url => {
            if (url) {
              this.urlList.push({
                fileName: parseURL(url).file,
                href: url
              })
            }
          })
          this.urlDialogVisible = true
        }
      })
    },
    handleToggleTab() {
      this.resetBlackSearchForm()
      this.$refs.form1.resetField()
      this.$refs.form2.resetField()
    },
    // 黑名单查询
    handleBlackSearch() {
      this.blackSearchDialogVisible = true
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        // if (j === 'realName' || j === 'userName') {
        //   return dataMasking(v[j])
        // }
        return v[j]
      }))
    },
    // 黑名单查询
    doBlackSearch(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.blackSearchLoading = true
          checkHitBlackList(this.blackSearchForm).then(res => {
            this.blackSearchLoading = false
            if (res.status === 200) {
              const content = res.rel ? this.$t('approvalDetail.hit') : this.$t('approvalDetail.nohit')
              this.$alert(content, this.$t('user.cue2'), {
                confirmButtonText: this.$t('btn.confirm')
              })
            }
          })
        }
      })
    },
    closeDialog(done) {
      done()
      this.resetBlackSearchForm()
    },
    resetBlackSearchForm() {
      this.blackSearchForm = {
        idNo: '',
        mobile: '',
        bankNo: ''
      }
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch('saveCustomPage', { path: this.$route.path, listQuery: this.listQuery });
      page(this.listQuery)
        .then(res => {
          if (res.status === 200) {
            this.expDis = false;
            this.listLoading = false;
            this.tableData = res.data.rows;
            this.total = Number(res.data.total);
          }
        })
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.authStatus = '';
      this.listQuery.realName = "";
      this.listQuery.phoneNo = "";
      this.listQuery.beginDate = this.GetDateStr(-6);
      this.listQuery.endDate = this.GetDateStr(0);
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList()
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.href {
  border-bottom: 1px solid #000;
  cursor: pointer;
  &:hover {
    color: #20a0ff;
    border-bottom: 1px solid #20a0ff;
  }
}
</style>